
# Chapter 8 example to illustrate how custom commands can be chained together
# 
# SPDX-License-Identifier: MIT

cmake_minimum_required(VERSION 3.23)

project(
  "ch8_create_tar_archive_example"
  VERSION 1.0
  DESCRIPTION "A simple C++ project to demonstrate appending two custom commands"
  LANGUAGES CXX)

# Create an executable target 
add_executable(
  ch8_create_tar_archive_example
  src/main.cpp)

# Add a custom command that creates a tar.gz archive from the output of the executable target ch8_create_tar_archive_example
add_custom_command(OUTPUT ch8_create_tar_archive_example.tar.gz
                  COMMAND cmake -E tar czf ${CMAKE_CURRENT_BINARY_DIR}/ch8_create_tar_archive_example.tar.gz $<TARGET_FILE:ch8_create_tar_archive_example>
                  COMMENT "Creating Archive for ch8_create_tar_archive_example"
                  VERBATIM 
)

# Add another custom command that adds another file to the archive, as this is using the same 
# OUTPUT as the first custom command the APPEND keyword is needed to chain the commands together
add_custom_command(OUTPUT ch8_create_tar_archive_example.tar.gz
  COMMAND cmake -E tar czf ${CMAKE_CURRENT_BINARY_DIR}/ch8_create_tar_archive_example.tar.gz ${CMAKE_CURRENT_SOURCE_DIR}/SomeFile.txt
  APPEND
) 

# Add a custom target that depends on the output generated by the custom commands
# if this target is built the custom commands are executed. As the first command depends on the 
# output of the executable target, the executable target will also be built first
add_custom_target(ch8_create_archive ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/ch8_create_tar_archive_example.tar.gz)

